﻿// Decompiled with JetBrains decompiler
// Type: MCM.MCMSubModule
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using BUTR.DependencyInjection.Logger;
using MCM.Abstractions;
using MCM.Abstractions.Properties;
using MCM.LightInject;
using System;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;

#nullable enable
namespace MCM
{
  public class MCMSubModule : MBSubModuleBase
  {
    private static IBUTRLogger<MCMSubModule> Logger = (IBUTRLogger<MCMSubModule>) new DefaultBUTRLogger<MCMSubModule>();
    private static readonly ServiceContainer LightInjectServiceContainer = new ServiceContainer((Action<ContainerOptions>) (options => options.EnableCurrentScope = false));

    public static MCMSubModule? Instance { get; private set; }

    private bool ServiceRegistrationWasCalled { get; set; }

    private bool OnBeforeInitialModuleScreenSetAsRootWasCalled { get; set; }

    public MCMSubModule()
    {
      MCMSubModule.Instance = this;
      BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer = new WithHistoryGenericServiceContainer((IGenericServiceContainer) new MCM.LightInject.LightInjectServiceContainer((IServiceContainer) MCMSubModule.LightInjectServiceContainer));
    }

    public void OnServiceRegistration()
    {
      this.ServiceRegistrationWasCalled = true;
      IGenericServiceContainer serviceContainer = this.GetServiceContainer();
      if (serviceContainer == null)
        return;
      serviceContainer.AddSettingsFormat<MemorySettingsFormat>();
      serviceContainer.AddSettingsFormat<MemorySettingsFormat>();
      serviceContainer.AddSettingsPropertyDiscoverer<NoneSettingsPropertyDiscoverer>();
      serviceContainer.AddTransient<IBUTRLogger, DefaultBUTRLogger>();
      serviceContainer.AddTransient(typeof (IBUTRLogger<>), typeof (DefaultBUTRLogger<>));
    }

    protected override void OnSubModuleLoad()
    {
      base.OnSubModuleLoad();
      if (this.ServiceRegistrationWasCalled)
        return;
      this.OnServiceRegistration();
    }

    protected override void OnSubModuleUnloaded()
    {
      base.OnSubModuleUnloaded();
      MCMSubModule.Instance = (MCMSubModule) null;
    }

    protected override void OnBeforeInitialModuleScreenSetAsRoot()
    {
      base.OnBeforeInitialModuleScreenSetAsRoot();
      if (this.OnBeforeInitialModuleScreenSetAsRootWasCalled)
        return;
      this.OnBeforeInitialModuleScreenSetAsRootWasCalled = true;
      GenericServiceProvider.GlobalServiceProvider = BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer.Build();
      MCMSubModule.Logger = GenericServiceProvider.GetService<IBUTRLogger<MCMSubModule>>() ?? MCMSubModule.Logger;
      BaseSettingsProvider.Instance = GenericServiceProvider.GetService<BaseSettingsProvider>();
    }

    public void OverrideServiceContainer(IGenericServiceContainer serviceContainer)
    {
      WithHistoryGenericServiceContainer serviceContainer1 = BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer;
      if (serviceContainer1 != null)
      {
        BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer = new WithHistoryGenericServiceContainer(serviceContainer);
        foreach (Action<IGenericServiceContainer> action in serviceContainer1.History)
          action((IGenericServiceContainer) BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer);
      }
      else
        BUTR.DependencyInjection.Extensions.ServiceCollectionExtensions.ServiceContainer = new WithHistoryGenericServiceContainer(serviceContainer);
    }

    public override void OnCampaignStart(Game game, object starterObject)
    {
      base.OnCampaignStart(game, starterObject);
      if (!(starterObject is CampaignGameStarter campaignGameStarter))
        return;
      campaignGameStarter.AddBehavior((CampaignBehaviorBase) new SettingsProviderCampaignBehavior(GenericServiceProvider.GetService<BaseSettingsProvider>()));
    }

    public override void OnMissionBehaviorInitialize(Mission mission)
    {
      base.OnMissionBehaviorInitialize(mission);
      mission.AddMissionBehavior((MissionBehavior) new SettingsProviderMissionBehavior(GenericServiceProvider.GetService<BaseSettingsProvider>()));
    }

    protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
    {
      base.OnGameStart(game, gameStarterObject);
      GenericServiceProvider.GameScopeServiceProvider = GenericServiceProvider.CreateScope();
    }

    public override void OnGameEnd(Game game)
    {
      base.OnGameEnd(game);
      GenericServiceProvider.GameScopeServiceProvider?.Dispose();
      GenericServiceProvider.GameScopeServiceProvider = (IGenericServiceProviderScope) null;
    }
  }
}
